$(function() {
	$("#loginsumbit").on('click', function() {

		var arr = [];
		sendForm($(this));
		//		window.location.href = "/admin/menu/index";

	});
	$('.send_message').on('click', function() {
		console.log($('.phone').val())
		var that=this 
		if($(this).data('click')) {
			var load = layer.load(0, {shade: false});
			var reg = /^1[34578]\d{9}$/g;
			var phoneformat =reg.test($('.phone').val());
			console.log(phoneformat)
			
			if(phoneformat) {
				console.log($('.phone').val())
				$.ajax({
					type: "post",
					url: "{:url('login/sms')}",
					data: {
						mobile: $('.phone').val()
					},
					success: function(res) {
						layer.close(load);
						console.log(res)
						if(res.code == 1) {
							Msg('发送成功');
							$(that).removeClass('am-btn-primary');
							$(that).addClass('am-btn-default');
							$(that).html(60);
							$(that).prop('disabled', true)
							$(that).data('click', false);
							
							var idx = setInterval(function() {
								var time = $(that).html() - 0;
								time -= 1;
								$(that).html(time);
								if($(that).html() == 0) {
									$(that).data('click', true);
									clearInterval(idx);
									$(that).prop('disabled', false)
									$(that).removeClass('am-btn-default');
									$(that).addClass('am-btn-primary');
									$(that).html('重新发送');
								}
							}, 1000);
						} else {
							Err(res.msg);
						}
					}
				})
			} else {
				layer.close(load);
				Err('手机格式不正确');
			}
		} else {
		}
	})
	$('.phone_login_sumbit').on('click', function() {
		if($(this).closest('form').find('input[name="sms_code"]').val().length == 6) {
			sendForm($(this));
		} else {
			Err('手机验证码有误');
		}
	})
})

function sendForm(formId, check, mode) {

	if(typeof formId == 'object') {
		var btn = formId;
		form = btn.closest('form');
		btn.addClass('disabled').data('txt', btn.text()).html("<i class='ace-icon fa fa-spinner fa-pulse'></i> 处理中...").prop('disabled', true);

	} else {
		form = $("#" + formId);
	}

	//检查
	check = check ? check : form.data('check');
	mode = mode ? mode : form.data('mode');
	console.log(checkForm(form))
	if(check && !checkForm(form)) {
		btn.removeClass('disabled').text(btn.data('txt')).prop('disabled', false);

		return false;
	}
	console.log('验证成功')
	post_url = form.prop('action');
	if(form.data('ajax')) {
		//		var vars = form.serialize();
		var vars = new FormData(form[0]);
		console.log(vars)
		$.ajax({
			type: "POST",
			url: post_url,
			data: vars,
			contentType: false,
			processData: false,
			//			dataType : "json",
			success: function(res) {
				console.log(res)
				rslt(res, mode, formId)

			}
		});
	} else {
		//		$("#" + formId).attr("action", post_url);
		if(!form.prop('method')) form.prop('method', 'post');
		//		form.submit();
	}
}

function checkForm(formId) {
	var check_flag = true;

	if(typeof formId == 'object') {
		form = formId;
	} else {
		form = $("#" + formId);
	}

	form.find(":input").each(function(i) {

		if($(this).data("check")) {
			var v;
			if($(this).prop('type') == 'checkbox' || $(this).prop('type') == 'radio') {
				v = $(this).prop('checked');
			} else {
				v = $(this).val();
			}
			if(!validate(v, $(this).data("check"))) {
				$(this).data("msg", $(this).data("msg") ? $(this).data("msg") : $(this).prop("placeholder"));
				if($(this).data("msg").indexOf("|")) {
					tmp = $(this).data("msg").split("|");
					msgtype = tmp[1];
					msg = tmp[0];
				}
				//				
				if(msgtype == 'popup') {
					LErr(tmp[0]);
				} else {
					Tips($(this).data("msg"), $(this), 1);
					$(this).focus();
					$(this).closest('label').addClass('has-error');
				}
				//提示
				check_flag = false;
				return check_flag;
			} else {
				//				$(this).closest('label').replaceClass('has-error','has-success');	
			}
		}
	})
	return check_flag;
}

function Tips(msg, target, err) {
	Err(msg ? msg : '操作失败，稍候再试');
}
/* 验证数据类型*/
function validate(data, datatype) {
	if(datatype.indexOf("|")) {
		tmp = datatype.split("|");
		datatype = tmp[0];
		data2 = tmp[1];
	}
	switch(datatype) {
		case "require":
			if(!data) {
				return false;
			} else {
				return true;
			}
			break;
		case "email":
			var reg = /^([0-9A-Za-z\-_\.]+)@([0-9a-z]+\.[a-z]{2,3}(\.[a-z]{2})?)$/g;
			return reg.test(data);
			break;
		case "mobile":
			var reg = /^1[34578]\d{9}$/g;
			return reg.test(data);
			break;
		case "number":
			var reg = /^[0-9]+\.{0,1}[0-9]{0,3}$/;
			return reg.test(data);
			break;
		case "html":
			var reg = /<...>/;
			return reg.test(data);
			break;
		case "gt":
			return data > data2
			break;
		case "notgt":
			return data <= data2
			break;
		case "lt":
			return data < data2
			break;
		case "notlt":
			return data >= data2
			break;
		case "eqt":
			data2 = $("#" + data2).val();
			return data >= data2
			break;
		case "password":
			var reg = /^.{6,16}$/;
			return reg.test(data);
			break;
		case "username":
			var reg = /^[a-z][0-9a-z]{4,}$/g;
			return reg.test(data);
			break;
		case "nickname":
			var reg = /^.{4,}$/g;
			return reg.test(data);
			break;
		case "code":
			var reg = /^[0-9]{4}$/;
			return reg.test(data);
			break;
	}
}

function goTo(url, newopen) {
	if(url == -1) {
		history.back();
	} else if(url == 0) {
		return false
	} else if(url == 1) {
		window.location.reload();
	} else if(url) {
		if(newopen) {
			window.open(url);
		} else {
			window.location.href = url;
		}

	}
}

function rslt(res, mode, formId) {
	var newopen = 0;
	var mode = 1;
	//	Load(1);

	if(res.code) {
		rmsg = res.msg ? res.msg : '操作成功！';
		url = res.url ? res.url : res.data.url;
		if(mode) {
			Msg(rmsg);
			goTo(url);
		} else {}

	} else {
		Err(res.msg ? res.msg : '操作失败，稍候再试');
		formId.removeClass('disabled').data('txt', formId.text()).html("提交").removeAttr('disabled');
	}
}

function goTo(url, newopen) {
	if(url == -1) {
		history.back();
	} else if(url == 0) {
		return false
	} else if(url == 1) {
		window.location.reload();
	} else if(url) {
		if(newopen) {
			window.open(url);
		} else {
			window.location.href = url;
		}

	}
}

function Msg(msg, func) {
	layer.msg(msg, {
		icon: 1
	}, func);
}

function Err(msg) {
	layer.msg(msg, {
		icon: 2
	});
}

function Load(close) {
	if(close) {
		layer.closeAll('loading');
	} else {
		layer.load(0, {
			shade: false,
		});
	}
}

function chang(a) {
	if(a.value != 0) {

		$(a).parent().next().html("")
	} else {

	}
}